package com.lhy.wcss.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhy.wcss.model.dto.PageDTO;
import com.lhy.wcss.model.dto.ProductDTO;
import com.lhy.wcss.model.vo.ProductVO;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lhy.wcss.common.JsonResponse;
import com.lhy.wcss.service.ProductService;
import com.lhy.wcss.model.domain.Product;


@Controller
@RequestMapping("/product")
public class ProductController {

    private final Logger logger = LoggerFactory.getLogger( ProductController.class );

    @Autowired
    private ProductService productService;

    @PostMapping("/save")
    @ResponseBody
    public JsonResponse save(@RequestBody ProductDTO productDTO){
        productService.mySave(productDTO);
        return JsonResponse.success(true);
    }
    /* 获取寒衣列表
     *
     *
     */
    @GetMapping("/productList")
    public String productList(PageDTO pageDTO, Model model){
        Page<ProductVO> page=productService.pageList(pageDTO);
        model.addAttribute("page",page);
        return "/page/products/productList";
    }
    /*
     * 描述：根据Id删除
     *
     */
    @GetMapping("removeById")
    @ResponseBody
    public JsonResponse removeById(Long id) {
        productService.removeById(id);
        return JsonResponse.success(null);
    }

    /*
     * 描述：根据Id 更新
     *
     */
    @PostMapping("update")
    @ResponseBody
    public JsonResponse update(Product product) {
        return JsonResponse.success(productService.updateById(product));
    }

    /**
     * 描述：根据Id 查询
     *
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Product product =  productService.getById(id);
        return JsonResponse.success(product);
    }
}

